<?php
/**
 * Discounts plugin class
 *
 * Main class for all taxes manipulations
 * @access 	public
 * @author 	Valentin
 * @copyright	ResMania 2009 all rights reserved.
 * @version	1.0
 * @link	http://developer.resmania.com/api
 * @since  	08-2009
 */
class RM_Plugin_Discounts extends RM_Plugin implements RM_Discounts_Plugin_Interface {
    /**
     * This string is for rm_reservation_summary table
     */
    const SUMMARY_TYPE = 'discount';

    /**
     * Save selected discounts to the database from user end reservation process
     *
     * @abstract
     * @param RM_Reservation_Row $detail
     * @param RM_Reservation_Details_Row $detailRow
     * @return bool
     */
    public function save(RM_Reservation_Details $detail, RM_Reservation_Details_Row $detailRow){
        $summaryModel = new RM_ReservationSummary();
        $discounts = $this->get($detail);
        foreach ($discounts as $discount) {
            $summaryModel->insert(array(
                'row_id' => $discount->id,
                'type' => self::SUMMARY_TYPE,
                'reservation_id' => null,
                'reservation_detail_id' => $detailRow->id,
                'total_amount' => -$discount->calculate($detail),
                'name' => $discount->getName()
            ));
        }
    }    

    /**
     * Returns extra HTML for a summary reservation page to present current discount plugin
     *
     * @abstract
     * @param RM_Reservation_Details $detail
     * @return void
     */
    public function getSummary(RM_Reservation_Details $detail){
        //We don't need this for simple automatic discounts, but this method will be usefull for coupons
    }

    /**
     * Get all discount object that are applied to current unit in the current reservation
     *
     * @abstract
     * @param RM_Reservation_Details $detail
     * @return array
     */
    public function get(RM_Reservation_Details $detail){        
        $model = new RM_Discounts();
        $discounts = $model->getByUnit($detail->getUnit());

        $resultDiscounts = array();
        foreach($discounts as $discount){
            if ($discount->isMatched($detail)){
                $resultDiscounts[] = $discount;
            }
        }
        return $resultDiscounts;
    }

    /**
     * Recalculate all for reservation, after some of the unit details has been changed
     *
     * @param RM_Reservation_Row $reservation
     * @return bool
     */
    function recalculate(RM_Reservation_Row $reservation){
        $summaryModel = new RM_ReservationSummary();        
        $details = $reservation->getDetails();
        foreach ($details as $detail) {
            $summaryRows = $summaryModel->fetchByReservationDetail($detail);
            //TODO: we need to think - maybe we should leave discount as is while recalculation process if
            //there was a summary row for unit detail.            
            foreach ($summaryRows as $summaryRow) {
                if ($summaryRow->type == self::SUMMARY_TYPE) {
                    $summaryRow->delete();
                }
            }            
            $reservationDetail = $detail->transform();
            $this->save($reservationDetail, $detail);
        }
    }

    /**
     * This method will return node object for main admin menu tree.
     * Every child classes could overload this method to return any of the node object.
     * If there is no need to present a plugin in the main admin tree overloaded method should return NULL
     *
     * @return stdClass | null
     */
    public function getNode(){
        $std = new stdClass;
        $std->id = $this->name.'_EditJson_NoAjax';
        $std->text = $this->getName();
        $std->leaf = 'true';
        $std->iconCls = 'RM_plugins_leaf_icon';
        return $std;
    }

    function getConfigNode(){
        return null;
    }

    /**
     * Public constructor
     */
    public function  __construct() {
        $this->name = 'Discounts';
    }

    /**
     * This method will invoke while adding new language to the Resmania
     *
     * @param string $iso ISO language code
     */
    public function addLanguage($iso)
    {
        parent::addLanguage($iso);
        $model = new RM_Discounts();
        $model->addLanguage($iso);
    }

    /**
     * Invokes after user delete language, make some changes for price module
     *
     * @param string $iso ISO language code
     * @return null
     */
    public function deleteLanguage($iso)
    {
        parent::deleteLanguage($iso);
        $model = new RM_Discounts();
        $model->deleteLanguage($iso);
    }

    public function install(){

        // *********
        // Languages
        // *********
        // add any non-base languages that are installed to the module
        $languageModel = new RM_Languages();
        $languages = $languageModel->fetchAllEnabled(); // get all installed languages
        $baseLanguages = array("en_GB","fr_FR"); // exclude array
        $db = Zend_Db_Table::getDefaultAdapter();

        foreach ($languages as $language) {
            if (!in_array($language->iso, $baseLanguages)){
                $db->query("ALTER TABLE `rm_discounts` ADD `".$language->iso."` TEXT NOT NULL");
            }
        }
    }
}
